<template>
	<view style="padding-bottom: 150rpx;">
		<view class="info-tabs">
			<view class='activeinfo' v-for="(item,index) in tabList" :key="index">
				<view :class="{texted:currentTab == index }" :data-tab="item.value" @click="switchTab(index)">
					{{item.name}}
				</view>
			</view>
		</view>
		<view class="list-box flex flex-wrap" v-if="photosList.length!=0">
			<view class="list-box-item" v-for="(item,index) in photosList" :key="index">
				<image src="/static/image/Index/1.png" mode=""></image>
				<image class="is-img" src="/static/image/mine/choose.png" mode=""></image>
			</view>
		</view>

		<view class="no-data-box" v-else>
			<view class="no-data-box-image">
				<image src="/static/image/mine/no-img.png" mode=""></image>
				<view class="">
					你还没有上传相册哦~
				</view>
			</view>
		</view>


		<view class="bts flex justify-between">
			<view class="btn" @tap="ChooseImage">
				上传相册
			</view>
			<view class="btn red">
				删除
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentTab: 0,
				tabList: [{
						name: '正常',
						value: 0
					},
					{
						name: '私密',
						value: 1
					},
				],
				imgLists: [],
				addImgLists: [], //上传图片
				photosList: [],
			}
		},
		onShow() {
			this.getPhotoList()
		},
		methods: {
			switchTab(index) {
				this.currentTab = index;
			},
			getPhotoList() {
				this.$http.getPhotoAlbumList({
					type: 'image'
				}).then(res => {
					if (res.code == '1') {
						this.photosList = res.data

					}
				})

			},
			ChooseImage() {
				let isUpload = true;
				let count = 9;
				isUpload = this.addImgLists.length < 9;
				count = 9 - this.addImgLists.length;
				if (isUpload) {
					uni.chooseImage({
						sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
						sourceType: ['camera', 'album'], //从相册选择
						count: count,
						success: res => {
							this.isUploadImg = true;
							uni.showLoading({
								title: '图片处理中...',
								mask: true
							});
							this.uploadimg({
								path: res.tempFilePaths,
								i: 0,
								success: 0,
								fail: 0
							});
						}
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: '最多只能上传九张图片',
						duration: 2000
					});
				}
			},
			//多张图片上传
			uploadimg(data) {
				let that = this;
				let i = data.i;
				let success = data.success;
				let fail = data.fail;
				if (i >= data.path.length) {
					// 所有图片都已上传完成
					uni.hideLoading();
					uni.showToast({
						icon: 'none',
						title: `上传完成，成功${success}张，失败${fail}张`
					});
					return;
				}

				uni.showLoading({
					title: '正在上传第' + (i + 1) + '/' + data.path.length,
					mask: true
				});

				uni.uploadFile({
					url: this.$http.baseUrl + '64803624da6de',
					filePath: data.path[i],
					name: 'file',
					success: resp => {
						uni.hideLoading();
						let tempResult = JSON.parse(resp.data);
						if (tempResult.code == '1') {
							success++;
							this.addImgLists.push(tempResult.data.url);
						} else {
							fail++;
							uni.showToast({
								icon: 'none',
								title: `第${i + 1}张图片上传失败: ${tempResult.msg}`
							});
						}
					},
					fail: err => {
						fail++;
						console.log('上传失败:', i + 1, err);
						uni.showToast({
							icon: 'none',
							title: `第${i + 1}张图片上传失败`
						});
					},
					complete: () => {
						// 继续上传下一张
						that.uploadimg({
							path: data.path,
							i: i + 1,
							success: success,
							fail: fail
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.info-tabs {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 108rpx;
		background: #f8f8f8;
		background-color: #fff;

		.activeinfo {
			font-size: 28rpx;
			line-height: 48rpx;
			margin: 0 60rpx;
		}

		.texted {
			font-size: 32rpx;
			font-weight: bold;
			border-bottom: 4rpx solid #000;
		}
	}

	.list-box {
		padding: 20rpx;

		.list-box-item {
			width: 219rpx;
			height: 316rpx;
			margin-right: 16rpx;
			margin-top: 16rpx;
			// margin:0 10rpx 20rpx 0;
			position: relative;

			image {
				width: 219rpx;
				height: 316rpx;
			}

			.is-img {
				position: absolute;
				right: 10rpx;
				top: 10rpx;
				width: 36rpx;
				height: 36rpx;
			}
		}
	}

	.no-data-box {
		margin-top: 150rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #999999;

		.no-data-box-image {
			width: 270rpx;
			margin: 0 auto;

			image {
				width: 100%;
				height: 310rpx;
				margin-bottom: 50rpx;
			}
		}

		.no-data-box-btn {
			width: 670rpx;
			height: 90rpx;
			border-radius: 50rpx;
			line-height: 90rpx;
			text-align: center;
			font-weight: 400;
			font-size: 32rpx;
			color: #FEC92F;
			background-color: #000;
			margin: 0 auto;
			margin-top: 150rpx;
		}
	}

	.bts {
		position: fixed;
		bottom: 20rpx;
		width: 690rpx;
		left: 50%;
		transform: translate(-50%);

		.btn {
			width: 50%;
			height: 90rpx;
			background-color: #000;
			line-height: 90rpx;
			text-align: center;
			font-weight: 400;
			font-size: 32rpx;
			color: #FEC92F;
			border-radius: 20rpx;
		}

		.red {
			background-color: #f00;
			color: #fff;
			margin-left: 15rpx;
		}
	}
</style>